// Declaring Global Variables
var db;
var handle;

//===================================Check if User Has Google Gears============================
if(window.google && google.gears)
{
    handle = google.gears.factory;
    //alert(handle.hasPermission);
    db = handle.create('beta.database');
    
    //Creating a new Database
    db.open('wall');
	//Creating a new Table
	db.execute('create table if not exists Best' + '(msg text)');
}
else
{
	// Download and install Google GEARS
    if (confirm("This Application requires Google Gears. Install now?")) 
    {
		// Use an absolute URL to allow this to work when run from a local file.
        location.href = "http://gears.google.com/?action=install&message=The Wall requires Google Gears to store your Messages." + "&return=code.google.com/p/code2infinity";
	}
}

//=====================================Store Message inside DB================================
function save(message)
{
	//Database Query to save the message
	db.execute('insert into Best values (?)', [message]);
	//alert("Message Saved: "+message);
}

//=====================================Retrieve Previously Stored Messages==========================
function firstLoad()
{
	//Select all the messages from the Database
	var rs = db.execute('select * from Best');

	while (rs.isValidRow())
	{
 			   message = rs.field(0);
 			   putRow(message);
  				rs.next();
	}
}
//=====================================Delete Record========================================
function remove(row)
{
	temp = document.getElementById('wall').rows[row].cells[0].innerHTML;
	//extracting the message from garbage charachters
	buff = temp.length-7;
	temp=temp.substr(3,buff);
	//alert("The message is: "+temp);	
	db.execute('delete from Best where msg=?', [temp]);
}